package com.dmagic.cms.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dmagic.cms.bean.HeatMapFormBean;
import com.dmagic.cms.common.annotation.FormModel;
import com.dmagic.cms.common.annotation.RequestJsonParam;
import com.dmagic.cms.common.bean.MapWapper;
import com.dmagic.cms.common.controller.CommonController;
import com.dmagic.cms.common.exception.ServiceException;
import com.dmagic.cms.common.query.ConditionsBean;
import com.dmagic.cms.common.query.PageUtil;
import com.dmagic.cms.common.util.JsonUtil;
import com.dmagic.cms.entity.HeatMap;
import com.dmagic.cms.entity.LinkedHeatMap;
import com.dmagic.cms.service.BaseService;
import com.dmagic.cms.service.HeatMapService;
import com.google.common.collect.Lists;

@Controller
@RequestMapping(value = "/home")
public class HomeController extends CommonController {

	private String HOME_PAGE = "home/heatMap";

	static {
	}

	@Autowired
	private BaseService baseService;

	@Autowired
	private HeatMapService heatMapService;

	@RequestMapping("heatMap")
	public String heatMapManager(Model model) {
		model.addAttribute("language", baseService.getAllLanguages());
		return HOME_PAGE;
	}

	@RequestMapping("heatMap/{linkId}")
	@ResponseBody
	public String setLinkHeatMap(@PathVariable("linkId") Long linkId, @RequestParam("type") Integer type,
			@RequestParam("isHot") Boolean isHot, RedirectAttributes redirectAttributes) throws Exception {
		LinkedHeatMap linkedHeatMap = new LinkedHeatMap();
		linkedHeatMap.setLinkedHeatMapLinkedId(linkId);
		linkedHeatMap.setLinkedHeatMapType(type);
		if (isHot == null) {
			linkedHeatMap.setLinkedHeatMapStatus(false);
		} else {
			linkedHeatMap.setLinkedHeatMapStatus(!isHot);
		}
		heatMapService.setLinkedHeatMap(linkedHeatMap);
		return "Successful.";
	}

	@RequestMapping(value = "heatMap/getAll", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String getAllHeatMap() {
		List<HeatMapFormBean> images = heatMapService.findUploadFileInfoImage();
		return JsonUtil.toJson(images);
	}

	@RequestMapping(value = "heatMap/search", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String search(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
			@RequestParam String orderType, @RequestParam String orderBy,
			@RequestJsonParam("rules") List<ConditionsBean> conditions) throws Exception {
		PageUtil pageUtil = new PageUtil();
		pageUtil.setAutoCount(true);
		pageUtil.setPageNo(pageNo);
		pageUtil.setPageSize(pageSize);
		pageUtil.setOrder(orderType);
		pageUtil.setOrderBy(orderBy);
		pageUtil.setConditions(conditions);
		List<HeatMapFormBean> heatMaps = heatMapService.searchHeatMap(pageUtil);
		pageUtil.setResult(heatMaps);
		return JsonUtil.toFullJson(pageUtil);
	}

	@RequestMapping("heatMap/save")
	public String saveHeatMapStatusAndDesc(@FormModel("heatMap") MapWapper<Integer, HeatMap> heatMaps,
			RedirectAttributes redirectAttributes) {

		try {
			List<HeatMap> heatMapList = Lists.newArrayList();
			for (Map.Entry<Integer, HeatMap> map : heatMaps.entrySet()) {
				heatMapList.add(map.getValue());
			}
			heatMapService.saveHeatMaps(heatMapList);
			redirectAttributes.addFlashAttribute(SUCCESS, "Save Successful.");
		} catch (ServiceException e) {
			logger.error("", e);
			redirectAttributes.addFlashAttribute(WARNNING, e.getMessage());
		}
		return "redirect:/home/heatMap.do";
	}

	@RequestMapping(value = "heatMap/deleteHeatMapFileInfo", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String deleteHeatMapFileInfo(@RequestParam Long heatMapId) {
		HeatMap heatMap = heatMapService.getHeatMap(heatMapId);
		heatMap.setFileInfoId(null);
		List<HeatMap> heatMaps = Lists.newArrayList();
		heatMaps.add(heatMap);
		heatMapService.saveHeatMaps(heatMaps);
		return JsonUtil.toJson(heatMap);
	}

	@RequestMapping(value = "web/heatMap/queryHeatMap", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryHomePageHeatMap(@RequestParam Long languageId) {
		List heatMaps = heatMapService.queryHeatMapInfo(languageId);
		return JsonUtil.toJson(heatMaps);
	}

}
